<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div style="margin-bottom: 10px">
        <el-button slot="right" class="filter-item" :disabled="crud.order.orderStatus==='PASS' || crud.order.orderStatus==='CLOSE'" size="mini"
                   type="primary" @click="submit()">
          保存
        </el-button>
        <el-button slot="right" class="filter-item" size="mini" type="success"
                   :disabled="crud.order.orderStatus !== 'SAVE'"
                   :loading="crud.passLoading" @click="crud.doDetailPassOrReject('PASS',id)">
          审核
        </el-button>
        <el-button slot="right" class="filter-item" size="mini" type="warning"
                   @click="copyOrder" v-permission="permission.copy">
          复制
        </el-button>
        <el-button slot="right" class="filter-item" size="mini" type="info" :loading="crud.rejectLoading"
                   :disabled="crud.order.orderStatus !== 'PASS'" @click="crud.doDetailPassOrReject('REJECT',id)">
          反审
        </el-button>
        <el-button
          slot="right"
          :loading="crud.downloadLoading"
          class="filter-item"
          size="mini"
          type="warning"
          @click="crud.preView"
          :disabled="$route.params.id == 'add'"
        >打印
        </el-button>
        <el-button
          slot="right"
          :loading="crud.downloadLoading"
          class="filter-item"
          size="mini"
          type="warning"
          @click="crud.preView1"
          :disabled="$route.params.id == 'add'"
        >打印预览
        </el-button>
        <el-button slot="right" class="filter-item" size="mini"
                   :disabled="crud.order.orderStatus !== 'PASS' || crud.order.feeContractGiftDto.isGenerate === 1"
                   type="info" :loading="crud.rejectLoading" @click="generalGift">
          生成赠品
        </el-button>
      </div>
      <el-form style="margin-bottom: 10px" size="small" label-width="120px" ref="orderForm" :rules="rules"
               :model="crud.order"
               >
        <el-row :gutter="10">
          <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
            <el-form-item label="合同开始时间" prop="startDate">
              <el-date-picker
                v-model="crud.order.startDate"
                type="date"
                :disabled="crud.order.orderStatus==='PASS' || crud.order.orderStatus==='CLOSE'"
                format="yyyy - MM - dd"
                value-format="yyyy-MM-dd"
                style="width: 100%;"
                default-value
                placeholder="请选择合同开始时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
            <el-form-item label="合同结束时间" prop="endDate">
              <el-date-picker
                v-model="crud.order.endDate"
                type="date"
                :disabled="crud.order.orderStatus==='PASS' || crud.order.orderStatus==='CLOSE'"
                format="yyyy - MM - dd"
                value-format="yyyy-MM-dd"
                style="width: 100%;"
                default-value
                placeholder="请选择合同结束时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
            <el-form-item label="单据编号">
              <el-input :disabled="true" v-model="crud.order.code" placeholder="系统自动生成"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
            <el-form-item label="合同名称" prop="name">
              <el-input :disabled="crud.order.orderStatus==='PASS' || crud.order.orderStatus==='CLOSE'" v-model="crud.order.name" placeholder="请输入合同名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
            <el-form-item label="合同类型" prop="type">
              <el-select :disabled="crud.order.orderStatus==='PASS' || crud.order.orderStatus==='CLOSE'" v-model="crud.order.type" class="filter-item" clearable placeholder="请选择合同类型" size="small"
                         style="width: 100%;">
                <el-option
                  v-for="item in dict.t_fee_order_type"
                  :key="item.id"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
            <el-form-item label="供应商">
              <zSelect
                :disabled="crud.order.orderStatus==='PASS' || crud.order.orderStatus==='CLOSE'"
                :value.sync="crud.order.supplierId ==0?null:crud.order.supplierId" :showName="crud.order.supplierName"
                       url="api/dropDown/basCustomerList"
                       :params="{isContact:'1,2'}"
                       style="width:  100%"
                       @change="changeSupplier"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
            <el-form-item label="客户">
              <zSelect
                :disabled="crud.order.orderStatus==='PASS' || crud.order.orderStatus==='CLOSE'"
                :value.sync="crud.order.customerId==0?null:crud.order.customerId" :showName="crud.order.customerName"
                       url="api/dropDown/basCustomerList"
                       :params="{isContact:'0,2'}"
                       style="width:  100%"
                       @change="changeCustomer"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
            <el-form-item label="取值对象" prop="valueType">
              <el-select
                :disabled="crud.order.orderStatus==='PASS' || crud.order.orderStatus==='CLOSE'"
                v-model="crud.order.feeContractGiftDto.valueType" class="filter-item" clearable
                         placeholder="请选择取值对象" size="small"
                         style="width: 100%;">
                <el-option
                  v-for="item in dict.t_fee_contract_value_type"
                  :key="item.id"
                  :label="item.label"
                  :value="item.value+''"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
            <el-form-item label="满足条件">
              <el-select
                :disabled="crud.order.orderStatus==='PASS' || crud.order.orderStatus==='CLOSE'"
                v-model="crud.order.feeContractGiftDto.reachItem" class="filter-item" clearable
                         placeholder="请选择满足条件" size="small"
                         style="width: 100%;"
                         @change="changeReachItem"
              >
                <el-option
                  v-for="item in dict.t_fee_contract_reach_item"
                  :key="item.id"
                  :label="item.label"
                  :value="item.value+''"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
            <el-form-item label="赠品条件">
              <el-select
                :disabled="crud.order.orderStatus==='PASS' || crud.order.orderStatus==='CLOSE'"
                v-model="crud.order.feeContractGiftDto.giftItem" class="filter-item" clearable
                         placeholder="请选择赠品条件" size="small"
                         style="width: 100%;"
                         @change="changeGiftItem"
              >
                <el-option
                  v-for="item in dict.t_fee_contract_gift_item"
                  :key="item.id"
                  :label="item.label"
                  :value="item.value+''"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
            <el-form-item label="赠品数量">
              <el-input-number
                :disabled="true"
                style="width:100%"
                v-model="crud.order.feeContractGiftDto.giftGoodsCount"
                :precision="0"
                :step="1"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
            <el-form-item label="状态">
              <el-select :disabled="true" v-model="crud.order.orderStatus" class="filter-item" clearable
                         placeholder="请选择状态" size="small"
                         style="width: 100%;">
                <el-option
                  v-for="item in dict.common_order_status"
                  :key="item.id"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
            <el-form-item label="备注">
              <el-input v-model="crud.order.remark"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-row :gutter="10">
        <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
          <el-card>
            <div slot="header">
              <span>条件项目</span>
              <el-button style="float: right;" type="primary" :disabled="crud.order.orderStatus==='PASS' || crud.order.orderStatus==='CLOSE'" @click="addProject">
                新增
              </el-button>
            </div>
            <el-form style="margin-bottom: 10px" size="small" label-width="90px"
                     :model="crud.order"
                     :disabled="crud.order.orderStatus==='PASS'">
              <el-row :gutter="10">
                <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                  <el-form-item label="达成条件">
                    <el-select v-model="crud.order.feeContractGiftDto.reachWay" class="filter-item" clearable
                               :filterable="true"
                               placeholder="数量/箱数/金额" size="small"
                               style="width: 100%;" @change="changeReachGiftWay">
                      <el-option
                        :disabled="crud.order.orderStatus==='PASS' || crud.order.orderStatus==='CLOSE'"
                        v-for="item in dict.t_fee_gift_reach_way"
                        :key="item.id"
                        :label="item.label"
                        :value="item.value+''"
                      />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                  <el-form-item label="达成目标">
                    <el-input-number
                      :disabled="crud.order.orderStatus==='PASS' || crud.order.orderStatus==='CLOSE'"
                      style="width:100%"
                      v-model="crud.order.feeContractGiftDto.reachValue"
                      :precision="reachPrecision"
                      :step="1"
                    />
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
            <!--表格渲染-->
            <el-table v-if="this.crud.order.feeContractGiftDto.reachItem === 2" ref="table" stripe border
                      v-loading="crud.loading"
                      :data="crud.order.feeContractGiftDto.feeContractGiftReachItemDtoList" size="small"
                      style="width: 100%;"
                      @selection-change="crud.selectionChangeHandler">
              <el-table-column type="selection" width="55"/>
              <el-table-column type="index" width="60" label="序号"/>
              <el-table-column v-if="columns.visible('goodsBrandCode')" prop="goodsCode" label="编码"/>
              <el-table-column v-if="columns.visible('goodsBrandName')" prop="goodsName" label="名称">
                <template slot-scope="{row}">
                  <zSelect :value.sync="row.goodsBrandId"
                           :disabled="crud.order.orderStatus==='PASS' || crud.order.orderStatus==='CLOSE'"
                           :filterable="true"
                           :showName="row.goodsBrandName"
                           :labelKey="'goodsBrandName'"
                           :valueKey="'goodsBrandId'"
                           :json="reachDetailList"
                           @change="changeReachBrands($event,row)"
                           style="width:  100%"/>
                </template>
              </el-table-column>
              <el-table-column v-if="columns.visible('barcode')" prop="barcode" label="条形码"/>
              <el-table-column :render-header="crud.renderHeader" label="操作" fixed="right" align="center">
                <template slot-scope="scope">
                  <el-button
                    slot="reference" class="el-icon-delete opration_icon_btn"
                    :disabled="crud.order.orderStatus==='PASS' || crud.order.orderStatus==='CLOSE'"
                    style="border: none;width: auto;height: auto"
                    @click="delProjectGoodsRow(scope.$index)"></el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-table v-else ref="table" stripe border v-loading="crud.loading"
                      :data="crud.order.feeContractGiftDto.feeContractGiftReachItemDtoList" size="small"
                      style="width: 100%;"
                      @selection-change="crud.selectionChangeHandler">
              <el-table-column type="selection" width="55"/>
              <el-table-column type="index" width="60" label="序号"/>
              <el-table-column v-if="columns.visible('goodsCode')" prop="goodsCode" label="编码"/>
              <el-table-column v-if="columns.visible('goodsName')" prop="goodsName" label="名称">
                <template slot-scope="{row}">
                  <zSelect :value.sync="row.goodsId"
                           :showName="row.goodsName"
                           :filterable="true"
                           :disabled="crud.order.orderStatus==='PASS' || crud.order.orderStatus==='CLOSE'"
                           :labelKey="'goodsName'"
                           :valueKey="'goodsId'"
                           :json="reachDetailList"
                           @change="changeReachGoods($event,row)"
                           style="width:  100%"/>
                </template>
              </el-table-column>
              <el-table-column v-if="columns.visible('barcode')" prop="barcode" label="条形码"/>
              <el-table-column :render-header="crud.renderHeader" label="操作" fixed="right" align="center">
                <template slot-scope="scope">
                  <el-button
                    slot="reference" class="el-icon-delete opration_icon_btn"
                    :disabled="crud.order.orderStatus==='PASS' || crud.order.orderStatus==='CLOSE'"
                    style="border: none;width: auto;height: auto"
                    @click="delProjectGoodsRow(scope.$index)"></el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-card>
        </el-col>
        <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
          <el-card>
            <div slot="header">
              <span>赠品项目</span>
              <el-button style="float: right;" :disabled="crud.order.orderStatus==='PASS' || crud.order.orderStatus==='CLOSE'" type="primary" @click="addGiftProject">
                新增
              </el-button>
            </div>
            <el-form style="margin-bottom: 10px" size="small" label-width="90px"
                     :model="crud.order"
                     :disabled="crud.order.orderStatus==='PASS' || crud.order.orderStatus==='CLOSE'">
              <el-row :gutter="10">
                <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                  <el-form-item label="赠送条件">
                    <el-select v-model="crud.order.feeContractGiftDto.giftWay" class="filter-item" clearable
                               placeholder="数量/箱数" size="small"
                               style="width: 100%;">
                      <el-option
                        v-for="item in dict.t_fee_gift_gift_way"
                        :key="item.id"
                        :label="item.label"
                        :value="item.value+''"
                      />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                  <el-form-item label="赠送目标">
                    <el-input-number
                      style="width:100%"
                      v-model="crud.order.feeContractGiftDto.giftValue"
                      :precision="0"
                      :step="1"
                    />
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
            <!--表格渲染-->
            <el-table v-if="crud.order.feeContractGiftDto.giftItem === 2" ref="table" stripe border
                      v-loading="crud.loading"
                      :data="crud.order.feeContractGiftDto.feeContractGiftGiftItemDtoList" size="small"
                      style="width: 100%;"
                      @selection-change="crud.selectionChangeHandler">
              <el-table-column type="selection" width="55"/>
              <el-table-column type="index" width="60" label="序号"/>
              <el-table-column v-if="columns.visible('goodsBrandCode')" prop="goodsCode" label="编码"/>
              <el-table-column v-if="columns.visible('goodsBrandName')" prop="goodsName" label="名称">
                <template slot-scope="{row}">
                  <zSelect :value.sync="row.goodsBrandId"
                           :showName="row.goodsBrandName"
                           :filterable ="true"
                           :disabled="crud.order.orderStatus==='PASS' || crud.order.orderStatus==='CLOSE'"
                           :labelKey="'goodsBrandName'"
                           :valueKey="'goodsBrandId'"
                           :json="giftDetailList"
                           @change="changeGiftBrands($event,row)"
                           style="width:  100%"/>
                </template>
              </el-table-column>
              <el-table-column v-if="columns.visible('barcode')" prop="barcode" label="条形码"/>
              <el-table-column :render-header="crud.renderHeader" label="操作" fixed="right" align="center">
                <template slot-scope="scope">
                  <el-button
                    slot="reference" class="el-icon-delete opration_icon_btn"
                    :disabled="crud.order.orderStatus==='PASS' || crud.order.orderStatus==='CLOSE'"
                    style="border: none;width: auto;height: auto"
                    @click="delGiftGoodsRow(scope.$index)"></el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-table v-else ref="table" stripe border v-loading="crud.loading"
                      :data="crud.order.feeContractGiftDto.feeContractGiftGiftItemDtoList" size="small"
                      style="width: 100%;"
                      @selection-change="crud.selectionChangeHandler">
              <el-table-column type="selection" width="55"/>
              <el-table-column type="index" width="60" label="序号"/>
              <el-table-column v-if="columns.visible('goodsCode')" prop="goodsCode" label="编码"/>
              <el-table-column v-if="columns.visible('goodsName')" prop="goodsName" label="名称">
                <template slot-scope="{row}">
                  <zSelect :value.sync="row.goodsId"
                           :showName="row.goodsName"
                           :filterable ="true"
                           :disabled="crud.order.orderStatus==='PASS' || crud.order.orderStatus==='CLOSE'"
                           :labelKey="'goodsName'"
                           :valueKey="'goodsId'"
                           :json="giftDetailList"
                           @change="changeGiftGoods($event,row)"
                           style="width:  100%"/>
                </template>
              </el-table-column>
              <el-table-column v-if="columns.visible('barcode')" prop="barcode" label="条形码"/>
              <el-table-column :render-header="crud.renderHeader" label="操作" fixed="right" align="center">
                <template slot-scope="scope">
                  <el-button
                    slot="reference" class="el-icon-delete opration_icon_btn"
                    :disabled="crud.order.orderStatus==='PASS' || crud.order.orderStatus==='CLOSE'"
                    style="border: none;width: auto;height: auto"
                    @click="delGiftGoodsRow(scope.$index)"></el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-card>
        </el-col>
      </el-row>
      <el-dialog title="生成赠品单" :visible.sync="showGiftOrder" width="60%">
        <el-form style="margin-bottom: 10px" size="small" label-width="120px">
          <el-row :gutter="10">
            <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
              <el-form-item label="赠品数量">
                <el-input-number
                  :disabled="true"
                  style="width:100%"
                  v-model="order.giftGoodsCount"
                  :precision="0"
                  :step="1"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <el-table ref="table" stripe border height="300" :row-class-name="crud.tableRowClassName"
                  v-loading="feeGiftLoading"
                  :data="order.feeContractGiftGiftItemDtoList" size="small" style="width: 100%;">
          <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('goodsCode')" prop="goodsCode"
                           label="编码"/>
          <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('goodsName')" prop="goodsName"
                           label="商品名称"/>
          <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('barcode')"
                           prop="barcode"
                           label="条形码"/>
          <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('goodsCount')"
                           prop="goodsCount"
                           label="数量">
            <template slot-scope="{row}">
              <el-input-number
                style="width:100%"
                v-model="row.goodsCount"
                :precision="0"
                :step="1"
              />
            </template>
          </el-table-column>
        </el-table>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="showGiftOrder = false">取消</el-button>
          <el-button :loading="confirmLoading" type="primary" @click="feeGiftConfirm">确认</el-button>
        </div>
      </el-dialog>
      <el-dialog  title="打印预览"
                  destroy-on-close
                  v-dialogDrag
                  close-on-press-escape
                  :visible.sync="showPdf" append-to-body width="50vw">
        <j-pdf :src="pdfSrc" ></j-pdf>
      </el-dialog>
      <el-dialog
        title="请选择打印模板"
        :visible.sync="crud.printConfig.pringDialog"
        width="30%"
      >
        <jPrintConfig groupId="feeContract"
                      v-if="crud.printConfig.pringDialog"
                      @getPrintId="crud.detailedPrint"
                      :isPreview="crud.printConfig.isPreview"
        ></jPrintConfig>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import crudFeeContractGift from '@/api/erp/feeContractGift'
import CRUD, {crud, form, header, presenter} from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import crudFeeContract from "@/api/erp/feeContract";
import jPrintConfig from '@/components/jPrintConfig/jPrintConfig'
// crud交由presenter持有
const defaultCrud = CRUD({
  title: '费用合同买赠',
  url: 'api/feeContract/detail',
  sort: 'id,desc',
  params() {
    const href = location.href.split('/')
     if(href[5].includes('copyId')){
      return {
        id: href[5].replace("copyId=",""),
      }
    }else{
      return {
        id: href[href.length - 1] == 'add' ? 0 : href[href.length - 1],  page: 0, size: 999,
      }
    }
  },
  order: {
    feeContractGiftDto: {
      feeContractGiftGiftItemDtoList: [],
      feeContractGiftReachItemDtoList: []
    }
  },
  crudMethod: {...crudFeeContract}
})
const defaultForm = {
  id: null,
  orderId: null,
  valueType: null,
  reachItem: null,
  reachWay: null,
  reachValue: null,
  giftItem: null,
  giftWay: null,
  giftValue: null,
  giftGoodsCount: null,
  seq: null,
  remark: null,
  createUserId: null,
  createUserCode: null,
  createUserName: null,
  createTime: null,
  updateUserId: null,
  updateUserCode: null,
  updateUserName: null,
  updateTime: null
}
export default {
  name: 'feeContractGift',
  components: {pagination, crudOperation, rrOperation, udOperation, jPrintConfig},
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  dicts: ['t_fee_order_type', 'common_yes_or_no', 't_fee_contract_value_type', 't_fee_contract_reach_item', 'common_order_status','t_fee_contract_gift_item', 't_fee_gift_reach_way', 't_fee_gift_gift_way'],
  data() {
    return {

      permission: {
        add: ['admin', 'feeContract:add'],
        edit: ['admin', 'feeContract:edit'],
        del: ['admin', 'feeContract:del'],
        import: ['admin', 'feeContract:import'],
        export: ['admin', 'feeContract:export'],
        copy: ['admin', 'feeContract:copy'],
        upload: ['admin', 'feeContract:upload'],
        download: ['admin', 'feeContract:download'],
      },
      reachPrecision: 0,
      rules: {
        startDate: [
          {required: true, message: '合同开始时间不能为空', trigger: 'blur'}
        ],
        endDate: [
          {required: true, message: '合同结束时间不能为空', trigger: 'blur'}
        ],
        name: [
          {required: true, message: '合同名称不能为空', trigger: 'blur'}
        ],
        type: [
          {required: true, message: '合同类型不能为空', trigger: 'blur'}
        ],
        // valueType: [
        //   {required: true, message: '取值对象不能为空', trigger: 'blur'}
        // ],
      },
      showPdf: false,
      showGiftOrder: false,
      feeGiftLoading: false,
      order: {
        goodsCount: null,
        feeContractGiftGiftItemDtoList: [],
      },
      reachDetailList: "",
      giftDetailList: "",
      confirmLoading: false
    }
  },
  watch: {},
  destroyed() {
    this.crud.setItemInfo("feeContractGiftDetail_add")
  },
  created() {
    if (this.$route.params.id == 'add') {
      if (this.crud.getItemInfo("feeContractGiftDetail_add")) {
        return false
      }
      this.crud.order = {
        feeContractGiftDto: {
          feeContractGiftGiftItemDtoList: [],
          feeContractGiftReachItemDtoList: []
        }
      }
    }
    if (window.location.href.includes('copy')){
      this.crud.order.createUserId = null
      this.crud.order.updateUserId = null
      this.crud.order.closeUserId = null
      this.crud.order.auditUserUserId = null
    }
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
    },
    [CRUD.HOOK.afterRefresh](crud, form) {
      if (this.$route.params.id == 'add') {
        crud.order = {
          feeContractGiftDto: {
            feeContractGiftGiftItemDtoList: [],
            feeContractGiftReachItemDtoList: []
          }
        }
      }
      if (this.$route.params.id != 'add') {
        this.getFeeContractGiftReachItem(this.crud.order.feeContractGiftDto.reachItem, 1)
        this.getFeeContractGiftReachItem(this.crud.order.feeContractGiftDto.giftItem, 2)
      }
      const href = location.href.split('/')
      if(href[5].includes('copyId')){
        this.crud.order.orderStatus = 'SAVE'
        this.crud.order.generalGift = 0
      }
      this.id = href[href.length - 1] == 'add' ? 0 : href[href.length - 1]
    },
    submit() {
      this.$refs['orderForm'].validate(valid => {
        if (!valid) {
          return
        }
        var oDate1 = new Date(this.crud.order.startDate);
        var oDate2 = new Date(this.crud.order.endDate);
        if (oDate1.getTime() > oDate2.getTime()) {
          this.$message({
            message: '合同开始时间不能大于合同结束时间',
            type: 'warning'
          })
          return
        }
        this.save()
      });
    },
    save() {
      let that = this
      that.crud.loadingOpen()
      this.crud.order.project = 'gift'
      const href = location.href.split('/')
      if (href[5].includes('copyId')) {
        this.crud.order.id = 0
        this.crud.order.feeContractGiftDto.isGenerate = 0
        this.crud.order.feeContractGiftDto.id = null
        this.crud.order.feeContractGiftDto.orderId = null
        for (let i = 0; i < this.crud.order.feeContractGiftDto.feeContractGiftGiftItemDtoList.length; i++) {
          this.crud.order.feeContractGiftDto.feeContractGiftGiftItemDtoList[i].id = null
          this.crud.order.feeContractGiftDto.feeContractGiftGiftItemDtoList[i].orderId = null
        }
        for (let i = 0; i < this.crud.order.feeContractGiftDto.feeContractGiftReachItemDtoList.length; i++) {
          this.crud.order.feeContractGiftDto.feeContractGiftReachItemDtoList[i].id = null
          this.crud.order.feeContractGiftDto.feeContractGiftReachItemDtoList[i].orderId = null
        }
      }
      let params = this.crud.order
      if (this.crud.order.orderStatus === 'PASS')params.isEditWithoutReject = 1
      crudFeeContract.add(params).then(res => {
        if (res.success) {
          this.crud.order = res.data.order
          let id = this.crud.order.id
          if (this.$route.params.id == 'add' || href[5].includes('copyId')) {
            this.$store.dispatch("tagsView/delView", this.$route);
            this.$router.push({params: {id}})
            this.crud.cleanItemInfo("feeContractGiftDetail_add")
          }
          this.$message({
            message: '保存成功',
            type: 'success'
          })
          that.crud.loadingClose()
        } else {
          that.crud.loadingClose()
          this.$message({
            message: res.msg,
            type: 'warning'
          })
        }

      }).catch((res) => {
        that.crud.loadingClose()
      })
    },
    addProject() {
      if (!this.crud.order.feeContractGiftDto.reachItem) {
        this.$message.warning("请选择满足条件")
        return
      }
      const item = {}
      this.crud.order.feeContractGiftDto.feeContractGiftReachItemDtoList.push(item)
    },
    detailedPrint(val) {
      if (val !== null) {
        crud.printConfig.pringDialog = false
        const urlList = window.location.href.split('/')
        const id = urlList[urlList.length - 1 ].split('?')[0]
        crud.doPrint(val, [id], crud.printConfig.isPreview)
      } else {
        this.$message.error('请选择打印模板')
      }
    },
    addGiftProject() {
      if (!this.crud.order.feeContractGiftDto.giftItem) {
        this.$message.warning("请选择赠品条件")
        return
      }
      const item = {}
      this.crud.order.feeContractGiftDto.feeContractGiftGiftItemDtoList.push(item)
    },
    changeReachGoods(e, row) {
      row.barcode = e.barcode
      row.goodsName = e.goodsName
      row.goodsCode = e.goodsCode
    },
    changeReachBrands(e, row) {
      row.barcode = e.barcode
      row.goodsBrandName = e.goodsBrandName
      row.goodsBrandCode = e.goodsBrandCode
    },
    changeGiftGoods(e, row) {
      row.barcode = e.barcode
      row.goodsName = e.goodsName
      row.goodsCode = e.goodsCode
    },
    changeGiftBrands(e, row) {
      row.barcode = e.barcode
      row.goodsBrandName = e.goodsBrandName
      row.goodsBrandCode = e.goodsBrandCode
    },
    delProjectGoodsRow(i) {
      if (this.$route.params.id !== 'add') {
        this.$confirm('此操作将直接删除此条记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.crud.order.feeContractGiftDto.feeContractGiftReachItemDtoList.splice(i, 1)
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      } else {
        this.crud.order.feeContractGiftDto.feeContractGiftReachItemDtoList.splice(i, 1)
      }
    },
    delGiftGoodsRow(i) {
      if (this.$route.params.id !== 'add') {
        this.$confirm('此操作将直接删除此条记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.crud.order.feeContractGiftDto.feeContractGiftGiftItemDtoList.splice(i, 1)
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      } else {
        this.crud.order.feeContractGiftDto.feeContractGiftGiftItemDtoList.splice(i, 1)
      }
    },
    changeReachGiftWay(e) {
      if (e === '3') {
        this.reachPrecision = 2
      } else {
        this.reachPrecision = 0
      }
    },
    copyOrder(e) {
      //内部还款
      this.crud.toCopy(this.crud.order.id,"/fee/feeContractRebateDetail")
    },
    generalGift() {
      this.showGiftOrder = true
      this.crud.loadingOpen()
      this.getGiftGoodsCount()
      this.selectFeeContractGiftGoods()
      this.crud.loadingClose()
    },
    getGiftGoodsCount() {
      let params = {id: this.crud.order.feeContractGiftDto.id}
      crudFeeContractGift.getGiftGoodsCount(params).then(res => {
        if (res.success) {
          this.order.giftGoodsCount = res.data
        }
      })
    },
    selectFeeContractGiftGoods() {
      let params = {id: this.crud.order.feeContractGiftDto.id}
      crudFeeContractGift.selectFeeContractGiftGoods(params).then(res => {
        if (res.success) {
          this.order.feeContractGiftGiftItemDtoList = res.data
        }
      })
    },
    feeGiftConfirm() {
      let sumGoodsCount = 0.00
      for (let i = 0; i < this.order.feeContractGiftGiftItemDtoList.length; i++) {
        const{goodsCount} = this.order.feeContractGiftGiftItemDtoList[i]
        sumGoodsCount += goodsCount
      }
      if(this.order.giftGoodsCount !== sumGoodsCount){
        this.$message.error("商品数量总和必须等于赠品数量")
        return
      }
      this.confirmLoading = true
      const params = {
        id: this.crud.order.feeContractGiftDto.id,
        ...this.order
      }
      crudFeeContractGift.addGiftGoods(params).then(res => {
        if (res.success) {
          this.confirmLoading = false
          this.showGiftOrder = false
          this.crud.order.feeContractGiftDto.isGenerate = 1
          this.$message.success("生成成功")
        } else {
          this.confirmLoading = false
          this.$message.error("生成失败")
        }
      }).catch(() => {
        this.confirmLoading = false
        this.$message.error("生成失败")
      });
    },
    changeSupplier(e) {
      this.crud.order.supplierId = e.id
      this.crud.order.supplierName = e.name
    },
    changeCustomer(e) {
      this.crud.order.customerId = e.id
      this.crud.order.customerName = e.name
    },
    getFeeContractGiftReachItem(item, type) {
      let params = null
      if (type == 1) {
        params = {
          reachItem: item,
          type: type
        }
      } else {
        params = {
          giftItem: item,
          type: type
        }
      }
      crudFeeContractGift.getFeeContractGiftReachItem(params).then(res => {
        if (res.success) {
          if (type == 1) {
            this.reachDetailList = JSON.stringify(res.data)
          } else {
            this.giftDetailList = JSON.stringify(res.data)
          }
        } else {
          this.$message.error("获取条件失败")
        }
      }).catch(e => {
        this.$message.error("获取条件失败")
      })
    },
    changeReachItem(e) {
      this.getFeeContractGiftReachItem(e, 1)
    },
    changeGiftItem(e) {
      this.getFeeContractGiftReachItem(e, 2)
    },
  }
}
</script>

<style scoped>
.table-img {
  display: inline-block;
  text-align: center;
  background: #ccc;
  color: #fff;
  white-space: nowrap;
  position: relative;
  overflow: hidden;
  vertical-align: middle;
  width: 32px;
  height: 32px;
  line-height: 32px;
}
</style>
